﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class UserControl_ControlImportFromBrand : System.Web.UI.UserControl
{
    
    ClassExecute.ClassExecute dt;
    string str = webapp4U.Configuration.CurrentConnectionString;
    string error = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //lấy thông tin kho vào ddr
            BindLocation();
            //set ngày hiện hành cho txtdate
            txtDate.Text = DateTime.Now.ToString("dd/MM/yyyy");
        }
    }
    protected void imgBtnSave_Click(object sender, ImageClickEventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        try
        {
            string docno = "";
            if (txtSoChungTuNhapHang.Text == "" || txtSoChungTuNhapHang.Text == null)
            {
                docno = dt.GetDataDepend(str, "K_GetDocno", CommandType.StoredProcedure, ref error);
                txtSoChungTuNhapHang.Text = docno;
            }
            else
            {
                docno = txtSoChungTuNhapHang.Text;
            }
            dt.ExecuteSQl_bool(str, CommandType.StoredProcedure, "k_DeleteDuplicateVoucher", ref error,
                new SqlParameter("@docno", docno));

            for (int i = 0; i < grvProduct.Rows.Count; i++)
            {
                
                dt.ExecuteSQl_bool(str, CommandType.StoredProcedure, "k_insertVoucher", ref error,
                    new SqlParameter("@location", ddrLocation.SelectedValue.ToString()),
                    new SqlParameter("@sitecode", Session["sitecode"].ToString()),
                    new SqlParameter("@docno", docno),
                    new SqlParameter("@itemkey", grvProduct.Rows[i].Cells[1].Text),
                    new SqlParameter("@trntyp", "R"),
                    new SqlParameter("@docdate", txtDate.Text),
                    new SqlParameter("@stockqty", grvProduct.Rows[i].Cells[3].Text),
                    new SqlParameter("@stockcost", grvProduct.Rows[i].Cells[4].Text),
                    new SqlParameter("@SONo", txtSoChungTuXuatHang.Text.Trim()));

            }
            uscMsgBox1.AddMessage("Nghiệp vụ nhập hàng về thành công", uscMsgBox.enmMessageType.Success);
        }
        catch (Exception ex)
        {
            uscMsgBox1.AddMessage("Hệ thống đang bảo trì", uscMsgBox.enmMessageType.Success);
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }   
    }
    protected void imgBtnDelete_Click(object sender, ImageClickEventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        try
        {

            bool kq = dt.ExecuteSQl_bool(str, CommandType.StoredProcedure, "ls_deleteVoucher", ref error,
                 new SqlParameter("@mTrnNo", txtSoChungTuNhapHang.Text));
            if (kq)
            {
                ResetData();
                uscMsgBox1.AddMessage("Xóa số chứng từ thành công", uscMsgBox.enmMessageType.Attention);
            }
            else
            {
                uscMsgBox1.AddMessage("Hệ thống đang bảo trì", uscMsgBox.enmMessageType.Error);
            }
        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }   
    }
    protected void imgBtnFind_Click(object sender, ImageClickEventArgs e)
    {
        TabContainer1.ActiveTabIndex = 1;
    }
    protected void btSearch_Click(object sender, EventArgs e)
    {
        LoadGridSearch();
    }
    protected void grvSearch_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvSearch.PageIndex = e.NewPageIndex;
        LoadGridSearch();
    }
    void LoadGridSearch()
    {
        dt = new ClassExecute.ClassExecute();
        try
        {
            DataTable db = dt.Get_Data_DataTable(str, ref error, "ls_loadUnpostedVoucher", CommandType.StoredProcedure,
                new SqlParameter("@mSiteCode", Session["sitecode"].ToString()));
            if (db.Rows.Count > 0)
            {
                grvSearch.DataSource = db;             
            }
            else
            {
                grvSearch.DataSource = null;
            }
            grvSearch.DataBind();
        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }   

    }
    void BindLocation()
    {
        dt = new ClassExecute.ClassExecute();
        try
        {
            DataTable db = dt.Get_Data_DataTable(str, ref error, "K_GetLocation", CommandType.StoredProcedure,
                new SqlParameter("@SiteCode", Session["sitecode"].ToString()));
            dt.BindDropDownList(db, ddrLocation, "location", "description");
        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        } 
    }
    protected void btnChon_Click(object sender, EventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        string check = dt.GetDataDepend(str, "ls_veridateVoucher", CommandType.StoredProcedure, ref error,
                new SqlParameter("@SONo",txtSoChungTuXuatHang.Text));
        if (check != "OK")
        {
            uscMsgBox1.AddMessage(check, uscMsgBox.enmMessageType.Error);
        }
        else
        {

            DataTable db = dt.Get_Data_DataTable(str, ref error, "ls_getVoucher", CommandType.StoredProcedure,
                new SqlParameter("@mSONo", txtSoChungTuXuatHang.Text),
                new SqlParameter("@mSiteCode", Session["sitecode"].ToString()));
            if (db.Rows.Count > 0)
            {
                grvProduct.DataSource = db;
                grvProduct.DataBind();
                //tránh trường hợp nhập 2,3 đơn,khi hàng về field mTrnNo=book
                if (db.Rows[0]["mTrnNo"].ToString() == "Book")
                {

                }
                else
                {
                    //set số chứng từ nhập hàng
                    txtSoChungTuNhapHang.Text = db.Rows[0]["mTrnNo"].ToString();
                    try
                    {
                        ddrLocation.SelectedValue = db.Rows[0]["mLocation"].ToString();
                    }
                    catch
                    {
                        return;
                    }
                }
            }
            else
            {
                ResetData();
            }
        }
    }
    void ResetData()
    {
        txtSoChungTuNhapHang.Text = "";
        txtSoChungTuXuatHang.Text = "";
        txtDate.Text= DateTime.Now.ToString("dd/MM/yyyy");
        grvProduct.DataSource = null;
        grvProduct.DataBind();
    }
    protected void grvSearch_RowCommand(object sender, GridViewCommandEventArgs e)
    {
         dt=new ClassExecute.ClassExecute();
         try
         {
             if (e.CommandName == "chon")
             {
                 GridViewRow grv = ((Control)e.CommandSource).NamingContainer as GridViewRow;
                 Label lblMaSoChungTuNhapHang = (Label)grv.FindControl("lblMaSoChungTuNhapHang");
                 
                 DataTable db = dt.Get_Data_DataTable(str, ref error, "ls_findVoucher", CommandType.StoredProcedure,
                         new SqlParameter("@mTrnNo", lblMaSoChungTuNhapHang.Text),
                         new SqlParameter("@mSiteCode", Session["sitecode"].ToString()));

                 if (db.Rows.Count > 0)
                 {

                     txtSoChungTuNhapHang.Text = "";
                     grvProduct.DataSource = db;
                     grvProduct.DataBind();
                     if (db.Rows[0]["mTrnNo"].ToString() == "Book")
                     {

                     }
                     else
                     {
                         txtSoChungTuXuatHang.Text = db.Rows[0]["mSONo"].ToString();
                         txtSoChungTuNhapHang.Text = db.Rows[0]["mTrnNo"].ToString();
                         try
                         {
                             ddrLocation.SelectedValue = db.Rows[0]["mLocation"].ToString();
                         }
                         catch
                         {

                         }
                     }
                     TabContainer1.ActiveTabIndex = 0;
                 }
             }
         }
         catch (Exception ex)
         {

             string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
             dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
         }   
    }
}